package com.bluebench.bigul.service.processor;

import java.io.IOException;
import java.io.StringReader;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.bluebench.bigul.domain.Tag;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.html.simpleparser.HTMLWorker;

public class ParagraphPartProcessor extends GenericProcessor{
	
	String value;
	
	Map<String,String> formatting;
	
	public ParagraphPartProcessor() {
	
		formatting = new HashMap<String,String>();
		formatting.put(":b:", "<b>");
		formatting.put(":-b:", "</b>");
		formatting.put(":i:", "<i>");
		formatting.put(":-i:", "</i>");
		formatting.put(":u:", "<u>");
		formatting.put(":-u:", "</u>");
	}
	
	
	@Override
	public Object process() throws SecurityException, IllegalArgumentException,
			ClassNotFoundException, NoSuchMethodException,
			IllegalAccessException, InvocationTargetException,
			InstantiationException, NoSuchFieldException {
		
		Paragraph p = new Paragraph();
		try{
			List<com.itextpdf.text.Element> elements = HTMLWorker.parseToList(new StringReader(value),null);
			for(com.itextpdf.text.Element e : elements){
			 	p.add(e);
			}
		}catch(IOException e){
			System.out.println("IOException Raised : "+e);
		}		
		return p;
	}

	@Override
	public void set(Tag t) {		
		
	}

	public void set(String value){
		
		Set<String> keys = formatting.keySet();
		for(String key : keys){
			if(value.contains(key)){
				value = value.replace(key,formatting.get(key));
			}
		}
		
		this.value = value;
	}
	
}
